package xin.xihc.jba.sql;

import xin.xihc.utils.common.CommonUtil;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * KV键值对MAP
 *
 * @author Leo.Xi
 * @date 2020/3/5
 * @since 1.0
 **/
public class KV extends LinkedHashMap<String, Object> {

    private int index = 0;
    private String prefix = "w_kv_";

    public KV() {
    }

    public KV(String name) {
        if (CommonUtil.isNotNullEmpty(name)) {
            this.prefix = name + "_kv_";
        }
    }

    @Override
    @Deprecated
    public Object put(String key, Object value) {
        throw new RuntimeException("please use add() method.");
    }

    public String add(Object value) {
        String key = this.prefix + index++;
        super.put(key, value);
        return key;
    }

    public void merge(KV kv) {
        for (Map.Entry<String, Object> next : kv.entrySet()) {
            super.put(next.getKey(), next.getValue());
        }
    }

}
